/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Copyright (c): Johannes Kuhaupt
 * Licence: MIT
 */
package view;

import control.GuiManager;
import control.SettingsManager;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 *
 * @author Jojo
 */
public class BooEditorMainFrame extends javax.swing.JFrame {

    private static BooEditorMainFrame instance = null;
    CustomTabPanel activeTab;
    boolean booTaskRunning = false;
    java.util.ResourceBundle langBundle = GuiManager.getLangBundle();

    /**
     * Creates new form BooEditorMainFrame
     *
     * @param pFile
     * @param pGuiMan
     */
    private BooEditorMainFrame() {
        initComponents();
        initToolbarAndStatusbar();
        this.initKeyShortcuts();
        
        this.addComponentListener(new ResizeListener(this));

        //Icon
        Image icon = new ImageIcon(getClass().getResource("/icons/BooEditorLogoIcon.png")).getImage();
        this.setIconImage(icon);

        pack();

    }

    private void initToolbarAndStatusbar() {
        this.initToolbar();
        this.initStatusbar();
    }

    private void initStatusbar() {
        this.panelStatusBar.setPreferredSize(new Dimension(this.lbl_statusbarFilePath.getPreferredSize().width,
                this.lbl_statusbarFilePath.getPreferredSize().height + 5));
        this.lbl_statusbarFilePath.setText(this.lbl_statusbarFilePath.getText() + " "
                + langBundle.getString("MsgInfoNewFile"));
        this.lbl_statusbarInformation.setText(GuiManager.getLangBundle().getString("MsgWelcome"));
    }

    private void initToolbar() {
        //Toolbar
        this.toolbar.setFloatable(false);
        this.btn_options.setComponentPopupMenu(this.contextMenuOptions);
        this.btn_newFile.setMinimumSize(new Dimension(this.btn_newFile.getWidth()*2, this.btn_newFile.getHeight()));
        this.btn_newFile.setPreferredSize(new Dimension(this.btn_newFile.getWidth()*2, this.btn_newFile.getHeight()));
        this.btn_newFile.setSize(new Dimension(this.btn_newFile.getWidth()*2, this.btn_newFile.getHeight()));

        


    }
    public void initKeyShortcuts(){
        this.mItem_printFile.setAccelerator(KeyStroke.getKeyStroke(
                        KeyEvent.VK_P, KeyEvent.CTRL_DOWN_MASK));
        this.mItem_help.setAccelerator(KeyStroke.getKeyStroke(
                        KeyEvent.VK_F1, 0));
        this.mItem_exit.setAccelerator(KeyStroke.getKeyStroke(
                        KeyEvent.VK_F4, KeyEvent.ALT_DOWN_MASK));
        this.btn_newFile.setMnemonic('N');
                
    }

    public static BooEditorMainFrame getInstance() {
        if (instance == null) {
            instance = new BooEditorMainFrame();
        }
        return instance;
    }

    

    /**
     *
     */
    public void readSettingAndUpdateView() {
        this.setSize(new Dimension(SettingsManager.getIntSettingValue("FRAMEWIDTH"), SettingsManager.getIntSettingValue("FRAMEHEIGHT")));
        this.setLocation(SettingsManager.getIntSettingValue("FRAMEPOSITIONX"), SettingsManager.getIntSettingValue("FRAMEPOSITIONY"));

        if (!SettingsManager.getStringSettingValue("HIGHLIGHTINGTHEME").equals("default")) {
            this.goToDarkSide();
        } else {
            this.goToLightSide();
        }

        this.repaint();
    }

    private void goToDarkSide() {
        this.txtArea_Output.setForeground(new Color(0xE0E2E4));
        this.txtArea_Output.setBackground(new Color(0x293134));
    }

    public void setInfoTextStatusBar(String pText) {
        this.lbl_statusbarFilePath.setText(pText);
    }

    private void goToLightSide() {
        this.txtArea_Output.setForeground(new Color(0x000000));
        this.txtArea_Output.setBackground(new Color(0xffffff));
    }

    /**
     *
     */
    public void saveNewViewSettings() {
        SettingsManager.updateSettingsItem("FRAMEWIDTH", this.getWidth() + "");
        SettingsManager.updateSettingsItem("FRAMEHEIGHT", this.getHeight() + "");
        SettingsManager.updateSettingsItem("FRAMEPOSITIONX", ((int) this.getLocationOnScreen().x) + "");
        SettingsManager.updateSettingsItem("FRAMEPOSITIONY", ((int) this.getLocationOnScreen().y) + "");
    }

    private void handleCloseFileAction() {
        this.activeTab.handleCloseFileAction();
    }

    private void handleOpenFileAction() {
        this.activeTab.handleOpenFileAction();
    }

    private void handleNewFileAction() {
        this.activeTab.handleNewFileAction();
    }

    public void setTabTitle(String pTabTitle) {
        this.jTabbedPane1.setTitleAt(this.jTabbedPane1.getSelectedIndex(), pTabTitle);
    }

    private void handleSaveFileAction() {

        if (this.activeTab != this.jPanelOutput) {
            this.activeTab.handleSaveFileAction();
        }
    }

    public void setBooReady(boolean pReady) {
        if (pReady) {
            this.activateBooButton();
        } else {
            this.deactivateBooButton();
        }
    }

    /**
     *
     * @param pText
     */
    public void setzeAusgabe(String pText) {
        this.txtArea_Output.setText(pText);
    }

    public void createNewTab(String pTitle, JPanel pTabPanel) {
        jTabbedPane1.addTab(pTitle, new javax.swing.ImageIcon(getClass().getResource("/icons/page_white_wrench.png")), pTabPanel);
        jTabbedPane1.setSelectedIndex(this.jTabbedPane1.getTabCount() - 1);
        this.activeTab = (CustomTabPanel) jTabbedPane1.getSelectedComponent();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contexMenuOutput = new javax.swing.JPopupMenu();
        mItemDeleteOutput = new javax.swing.JMenuItem();
        mItemCopyOutput = new javax.swing.JMenuItem();
        contextMenuOptions = new javax.swing.JPopupMenu();
        mItemBigFont = new javax.swing.JMenuItem();
        mItem_printFile = new javax.swing.JMenuItem();
        mItem_EditorOptions = new javax.swing.JMenuItem();
        menu_LejosOptions = new javax.swing.JMenuItem();
        menu_Boo = new javax.swing.JMenu();
        mItemInterpret = new javax.swing.JMenuItem();
        mItem_Compile = new javax.swing.JMenuItem();
        mItem_CompileDll = new javax.swing.JMenuItem();
        mItem_about = new javax.swing.JMenuItem();
        mItem_help = new javax.swing.JMenuItem();
        mItem_exit = new javax.swing.JMenuItem();
        toolbar = new javax.swing.JToolBar();
        btn_newFile = new javax.swing.JButton();
        btn_openFile = new javax.swing.JButton();
        btn_close = new javax.swing.JButton();
        filler6 = new javax.swing.Box.Filler(new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 32767));
        btn_save = new javax.swing.JButton();
        btn_saveAs = new javax.swing.JButton();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 32767));
        btn_undo = new javax.swing.JButton();
        btn_redo = new javax.swing.JButton();
        filler3 = new javax.swing.Box.Filler(new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 32767));
        btn_copy = new javax.swing.JButton();
        btn_paste = new javax.swing.JButton();
        filler4 = new javax.swing.Box.Filler(new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 0), new java.awt.Dimension(20, 32767));
        btn_startBooAction = new javax.swing.JButton();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        btn_options = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelOutput = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtArea_Output = new javax.swing.JTextArea();
        panelStatusBar = new javax.swing.JPanel();
        lbl_statusbarFilePath = new javax.swing.JLabel();
        lbl_statusbarInformation = new javax.swing.JLabel();

        mItemDeleteOutput.setText(langBundle.getString("Delete"));
        mItemDeleteOutput.setToolTipText(langBundle.getString("TT_OutputDelete"));
        mItemDeleteOutput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mItemDeleteOutputActionPerformed(evt);
            }
        });
        contexMenuOutput.add(mItemDeleteOutput);

        mItemCopyOutput.setText(langBundle.getString("Copy"));
        mItemCopyOutput.setToolTipText(langBundle.getString("TT_OutputCopy"));
        mItemCopyOutput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mItemCopyOutputActionPerformed(evt);
            }
        });
        contexMenuOutput.add(mItemCopyOutput);

        mItemBigFont.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/magnifier.png"))); // NOI18N
        mItemBigFont.setText(langBundle.getString("BigScreenModus"));
        mItemBigFont.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mItemBigFontActionPerformed(evt);
            }
        });
        contextMenuOptions.add(mItemBigFont);

        mItem_printFile.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/printer.png"))); // NOI18N
        mItem_printFile.setText(langBundle.getString("Print"));
        mItem_printFile.setToolTipText(langBundle.getString("TT_Print"));
        mItem_printFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mItem_printFileActionPerformed(evt);
            }
        });
        contextMenuOptions.add(mItem_printFile);

        mItem_EditorOptions.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/wrench_orange.png"))); // NOI18N
        mItem_EditorOptions.setText(langBundle.getString("EditorOptions"));
        mItem_EditorOptions.setToolTipText(langBundle.getString("TT_OptionsEditor"));
        mItem_EditorOptions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mItem_EditorOptionsActionPerformed(evt);
            }
        });
        contextMenuOptions.add(mItem_EditorOptions);

        menu_LejosOptions.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/BooSettings.png"))); // NOI18N
        menu_LejosOptions.setText(langBundle.getString("BooOptions"));
        menu_LejosOptions.setToolTipText(langBundle.getString("TT_LejosOptions"));
        menu_LejosOptions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menu_LejosOptionsActionPerformed(evt);
            }
        });
        contextMenuOptions.add(menu_LejosOptions);

        menu_Boo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Boo.png"))); // NOI18N
        menu_Boo.setText(langBundle.getString("BooTools"));

        mItemInterpret.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Boo.png"))); // NOI18N
        mItemInterpret.setText(langBundle.getString("BooInterpret"));
        mItemInterpret.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mItemInterpretActionPerformed(evt);
            }
        });
        menu_Boo.add(mItemInterpret);

        mItem_Compile.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Boo.png"))); // NOI18N
        mItem_Compile.setText(langBundle.getString("BooCompile"));
        mItem_Compile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mItem_CompileActionPerformed(evt);
            }
        });
        menu_Boo.add(mItem_Compile);

        mItem_CompileDll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Boo.png"))); // NOI18N
        mItem_CompileDll.setText(langBundle.getString("BooCompileDll"));
        mItem_CompileDll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mItem_CompileDllActionPerformed(evt);
            }
        });
        menu_Boo.add(mItem_CompileDll);

        contextMenuOptions.add(menu_Boo);

        mItem_about.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/information.png"))); // NOI18N
        mItem_about.setText(langBundle.getString("About"));
        mItem_about.setToolTipText(langBundle.getString("TT_About"));
        mItem_about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mItem_aboutActionPerformed(evt);
            }
        });
        contextMenuOptions.add(mItem_about);

        mItem_help.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/help.png"))); // NOI18N
        mItem_help.setText(langBundle.getString("Help"));
        mItem_help.setToolTipText(langBundle.getString("TT_Help"));
        mItem_help.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mItem_helpActionPerformed(evt);
            }
        });
        contextMenuOptions.add(mItem_help);

        mItem_exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/door_out.png"))); // NOI18N
        mItem_exit.setText(langBundle.getString("Exit"));
        mItem_exit.setToolTipText(langBundle.getString("TT_Exit"));
        mItem_exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mItem_exitActionPerformed(evt);
            }
        });
        contextMenuOptions.add(mItem_exit);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(langBundle.getString("FrameTitle"));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                handleWindowClosing(evt);
            }
        });

        toolbar.setFloatable(false);
        toolbar.setRollover(true);
        toolbar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        btn_newFile.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/page.png"))); // NOI18N
        btn_newFile.setMnemonic('N');
        btn_newFile.setText(langBundle.getString("NewFile"));
        btn_newFile.setToolTipText(langBundle.getString("TT_NewFile"));
        btn_newFile.setFocusable(false);
        btn_newFile.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_newFile.setMaximumSize(null);
        btn_newFile.setMinimumSize(null);
        btn_newFile.setPreferredSize(null);
        btn_newFile.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_newFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_newFileActionPerformed(evt);
            }
        });
        toolbar.add(btn_newFile);

        btn_openFile.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/folder_page.png"))); // NOI18N
        btn_openFile.setText(langBundle.getString("OpenFile"));
        btn_openFile.setToolTipText(langBundle.getString("TT_OpenFile"));
        btn_openFile.setFocusable(false);
        btn_openFile.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_openFile.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_openFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_openFileActionPerformed(evt);
            }
        });
        toolbar.add(btn_openFile);

        btn_close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/page_delete.png"))); // NOI18N
        btn_close.setText(langBundle.getString("CloseFile"));
        btn_close.setToolTipText(langBundle.getString("TT_CloseFile"));
        btn_close.setFocusable(false);
        btn_close.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_close.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_closeActionPerformed(evt);
            }
        });
        toolbar.add(btn_close);
        toolbar.add(filler6);

        btn_save.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/page_save.png"))); // NOI18N
        btn_save.setText(langBundle.getString("SaveFile"));
        btn_save.setToolTipText(langBundle.getString("TT_SaveFile"));
        btn_save.setFocusable(false);
        btn_save.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_save.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_saveActionPerformed(evt);
            }
        });
        toolbar.add(btn_save);

        btn_saveAs.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/page_save.png"))); // NOI18N
        btn_saveAs.setText(langBundle.getString("SaveAs"));
        btn_saveAs.setToolTipText(langBundle.getString("TT_SaveFileAs"));
        btn_saveAs.setFocusable(false);
        btn_saveAs.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_saveAs.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_saveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_saveAsActionPerformed(evt);
            }
        });
        toolbar.add(btn_saveAs);
        toolbar.add(filler2);

        btn_undo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_undo.png"))); // NOI18N
        btn_undo.setText(langBundle.getString("Undo"));
        btn_undo.setToolTipText(langBundle.getString("TT_Undo"));
        btn_undo.setFocusable(false);
        btn_undo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_undo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_undo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_undoActionPerformed(evt);
            }
        });
        toolbar.add(btn_undo);

        btn_redo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_redo.png"))); // NOI18N
        btn_redo.setText(langBundle.getString("Redo"));
        btn_redo.setToolTipText(langBundle.getString("TT_Redo"));
        btn_redo.setFocusable(false);
        btn_redo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_redo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_redo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_redoActionPerformed(evt);
            }
        });
        toolbar.add(btn_redo);
        toolbar.add(filler3);

        btn_copy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/page_copy.png"))); // NOI18N
        btn_copy.setText(langBundle.getString("Copy"));
        btn_copy.setToolTipText(langBundle.getString("TT_Copy"));
        btn_copy.setFocusable(false);
        btn_copy.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_copy.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_copy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_copyActionPerformed(evt);
            }
        });
        toolbar.add(btn_copy);

        btn_paste.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/page_paste.png"))); // NOI18N
        btn_paste.setText(langBundle.getString("Paste"));
        btn_paste.setToolTipText(langBundle.getString("TT_Paste"));
        btn_paste.setFocusable(false);
        btn_paste.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_paste.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_paste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_pasteActionPerformed(evt);
            }
        });
        toolbar.add(btn_paste);
        toolbar.add(filler4);

        btn_startBooAction.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Boo.png"))); // NOI18N
        btn_startBooAction.setText(langBundle.getString("Interpret"));
        btn_startBooAction.setToolTipText(langBundle.getString("TT_StartCompiler"));
        btn_startBooAction.setFocusable(false);
        btn_startBooAction.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_startBooAction.setMaximumSize(new java.awt.Dimension(107, 41));
        btn_startBooAction.setMinimumSize(new java.awt.Dimension(107, 41));
        btn_startBooAction.setPreferredSize(new java.awt.Dimension(107, 41));
        btn_startBooAction.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_startBooAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_startBooActionActionPerformed(evt);
            }
        });
        toolbar.add(btn_startBooAction);
        toolbar.add(filler1);

        btn_options.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cog.png"))); // NOI18N
        btn_options.setText(langBundle.getString("Options"));
        btn_options.setToolTipText(langBundle.getString("TT_Optionen"));
        btn_options.setFocusable(false);
        btn_options.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btn_options.setMaximumSize(new java.awt.Dimension(107, 41));
        btn_options.setMinimumSize(new java.awt.Dimension(107, 41));
        btn_options.setPreferredSize(null);
        btn_options.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btn_options.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btn_optionsMousePressed(evt);
            }
        });
        toolbar.add(btn_options);

        getContentPane().add(toolbar, java.awt.BorderLayout.PAGE_START);

        jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                handleEventTabChanged(evt);
            }
        });

        jPanelOutput.setMinimumSize(new java.awt.Dimension(600, 50));
        jPanelOutput.setName("Ausgabe"); // NOI18N
        jPanelOutput.setPreferredSize(new java.awt.Dimension(792, 120));

        txtArea_Output.setEditable(false);
        txtArea_Output.setColumns(20);
        txtArea_Output.setRows(3);
        txtArea_Output.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        txtArea_Output.setComponentPopupMenu(contexMenuOutput);
        jScrollPane1.setViewportView(txtArea_Output);

        javax.swing.GroupLayout jPanelOutputLayout = new javax.swing.GroupLayout(jPanelOutput);
        jPanelOutput.setLayout(jPanelOutputLayout);
        jPanelOutputLayout.setHorizontalGroup(
            jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelOutputLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1257, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelOutputLayout.setVerticalGroup(
            jPanelOutputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 528, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(langBundle.getString("Output"), new javax.swing.ImageIcon(getClass().getResource("/icons/application_xp_terminal.png")), jPanelOutput); // NOI18N

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        panelStatusBar.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 5, 2, 2));
        panelStatusBar.setPreferredSize(new java.awt.Dimension(792, 20));
        panelStatusBar.setLayout(new java.awt.GridLayout(1, 2, 10, 0));

        lbl_statusbarFilePath.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lbl_statusbarFilePath.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/folder_page.png"))); // NOI18N
        lbl_statusbarFilePath.setText(langBundle.getString("StatusbarWelcome"));
        panelStatusBar.add(lbl_statusbarFilePath);

        lbl_statusbarInformation.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lbl_statusbarInformation.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/information.png"))); // NOI18N
        lbl_statusbarInformation.setText("jLabel1");
        panelStatusBar.add(lbl_statusbarInformation);

        getContentPane().add(panelStatusBar, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void showOutputTab() {
        this.jTabbedPane1.setSelectedIndex(0);
    }

    private void activateBooButton() {
        this.booTaskRunning = false;
        btn_startBooAction.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Boo.png")));
        btn_startBooAction.setText(langBundle.getString("Interpret"));
        btn_startBooAction.setToolTipText(langBundle.getString("TT_StartBooTask"));
        btn_startBooAction.setFocusable(false);
    }

    private void deactivateBooButton() {
        this.booTaskRunning = true;
        btn_startBooAction.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png")));
        btn_startBooAction.setText(langBundle.getString("CancelBoo"));
        btn_startBooAction.setToolTipText(langBundle.getString("TT_BtnStopBoo"));
        btn_startBooAction.setFocusable(false);
    }

    private void mItem_EditorOptionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mItem_EditorOptionsActionPerformed
        this.saveNewViewSettings();
        new EditorOptionDialog(this, true, GuiManager.getInstance());
    }//GEN-LAST:event_mItem_EditorOptionsActionPerformed

    private void mItemDeleteOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mItemDeleteOutputActionPerformed
        this.txtArea_Output.setText("");
    }//GEN-LAST:event_mItemDeleteOutputActionPerformed

    private void mItemCopyOutputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mItemCopyOutputActionPerformed
        StringSelection ss = new StringSelection(this.txtArea_Output.getText());
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    }//GEN-LAST:event_mItemCopyOutputActionPerformed

    private void handleWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_handleWindowClosing
        this.saveNewViewSettings();
        if(!SettingsManager.isReadOnly()){
            SettingsManager.writeSettingsToFile(SettingsManager.isPersonalConfigInUse());
        }
        this.handleCloseFileAction();
    }//GEN-LAST:event_handleWindowClosing

    private void handleEventTabChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_handleEventTabChanged
        if (this.jTabbedPane1.getSelectedComponent() != this.jPanelOutput) {
            this.activeTab = (CustomTabPanel) this.jTabbedPane1.getSelectedComponent();
            if (this.activeTab.isSavedFile()) {
                this.setInfoTextStatusBar(langBundle.getString("File") + ": " + this.activeTab.getFilePath());
            } else {
                this.setInfoTextStatusBar(langBundle.getString("MsgInfoNewFile"));
            }
        } else {
            this.setInfoTextStatusBar(langBundle.getString("Output"));
        }

    }//GEN-LAST:event_handleEventTabChanged

    private void mItem_printFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mItem_printFileActionPerformed
        try {
            this.activeTab.handlePrint();
        } catch (PrinterException ex) {
            GuiManager.getInstance().showErrorMessage(langBundle.getString("MsgErrorPrint"));
            Logger.getLogger(BooEditorMainFrame.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("Error, printing document failed: " + ex.toString());
        }
    }//GEN-LAST:event_mItem_printFileActionPerformed

    private void mItem_exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mItem_exitActionPerformed
        this.saveNewViewSettings();
        if(!SettingsManager.isReadOnly()){
            SettingsManager.writeSettingsToFile(SettingsManager.isPersonalConfigInUse());
        }
        
        this.dispose();
        System.exit(0);
    }//GEN-LAST:event_mItem_exitActionPerformed

    private void menu_LejosOptionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menu_LejosOptionsActionPerformed
        new BooOptionsDialog(this, true, GuiManager.getInstance());
    }//GEN-LAST:event_menu_LejosOptionsActionPerformed

    private void mItem_aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mItem_aboutActionPerformed
        new InfoDialog(this, true).setVisible(true);
    }//GEN-LAST:event_mItem_aboutActionPerformed

    private void mItem_helpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mItem_helpActionPerformed
        Desktop desk = Desktop.getDesktop();
        try {
            File f = new File(System.getProperty("user.dir")+File.separator+"readme.html");
            desk.open(f);
        } catch (Exception ex) {
            Logger.getLogger(BooEditorMainFrame.class.getName()).log(Level.SEVERE, null, ex);
            GuiManager.getInstance().showErrorMessage(langBundle.getString("MsgErrorOpenHelp"));
            System.err.println("Error beim Öffnen der Hilfedatei");
        }
    }//GEN-LAST:event_mItem_helpActionPerformed

    private void btn_newFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_newFileActionPerformed
        this.handleNewFileAction();
    }//GEN-LAST:event_btn_newFileActionPerformed

    private void btn_openFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_openFileActionPerformed
        this.handleOpenFileAction();
    }//GEN-LAST:event_btn_openFileActionPerformed

    private void btn_closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_closeActionPerformed
        this.handleCloseTab();
    }//GEN-LAST:event_btn_closeActionPerformed

    private void btn_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_saveActionPerformed
        this.handleSaveFileAction();
    }//GEN-LAST:event_btn_saveActionPerformed

    private void btn_saveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_saveAsActionPerformed
        this.activeTab.handleSaveFileAs();
    }//GEN-LAST:event_btn_saveAsActionPerformed

    private void btn_undoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_undoActionPerformed
        this.activeTab.handleUndoLastAction();
    }//GEN-LAST:event_btn_undoActionPerformed

    private void btn_redoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_redoActionPerformed
        this.activeTab.handleRedoLastAction();
    }//GEN-LAST:event_btn_redoActionPerformed

    private void btn_copyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_copyActionPerformed
        this.activeTab.handleCopy();
    }//GEN-LAST:event_btn_copyActionPerformed

    private void btn_pasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_pasteActionPerformed
        this.activeTab.handlePaste();
    }//GEN-LAST:event_btn_pasteActionPerformed

    private void btn_startBooActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_startBooActionActionPerformed
        this.txtArea_Output.setText("");
        if (!this.booTaskRunning) {
            this.activeTab.startBooInterpreter();
        } else {
            this.activeTab.stopBooTask();
        }
    }//GEN-LAST:event_btn_startBooActionActionPerformed

    private void mItemBigFontActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mItemBigFontActionPerformed
        GuiManager.setBigScreenModus( !GuiManager.bigScreenModus());
        if(GuiManager.bigScreenModus()){
            this.mItemBigFont.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/magifier_zoom_out.png")));
        }
        else{
            this.mItemBigFont.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/magnifier.png")));
        }
        
        GuiManager.getInstance().updateGui();
    }//GEN-LAST:event_mItemBigFontActionPerformed

    private void mItem_CompileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mItem_CompileActionPerformed
        this.txtArea_Output.setText("");
        if (!this.booTaskRunning) {
            this.activeTab.startBooCompiler(false);
        } else {
            this.activeTab.stopBooTask();
        }
    }//GEN-LAST:event_mItem_CompileActionPerformed

    private void mItem_CompileDllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mItem_CompileDllActionPerformed
        this.txtArea_Output.setText("");
        if (!this.booTaskRunning) {
            this.activeTab.startBooCompiler(true);
        } else {
            this.activeTab.stopBooTask();
        }
    }//GEN-LAST:event_mItem_CompileDllActionPerformed

    private void mItemInterpretActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mItemInterpretActionPerformed
        this.txtArea_Output.setText("");
        if (!this.booTaskRunning) {
            this.activeTab.startBooInterpreter();
        } else {
            this.activeTab.stopBooTask();
        }
    }//GEN-LAST:event_mItemInterpretActionPerformed

    private void btn_optionsMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_optionsMousePressed
            this.saveNewViewSettings();    
            this.contextMenuOptions.show(evt.getComponent(), evt.getX(), evt.getY());
            this.contextMenuOptions.setVisible(true);
    }//GEN-LAST:event_btn_optionsMousePressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_close;
    private javax.swing.JButton btn_copy;
    private javax.swing.JButton btn_newFile;
    private javax.swing.JButton btn_openFile;
    private javax.swing.JButton btn_options;
    private javax.swing.JButton btn_paste;
    private javax.swing.JButton btn_redo;
    private javax.swing.JButton btn_save;
    private javax.swing.JButton btn_saveAs;
    private javax.swing.JButton btn_startBooAction;
    private javax.swing.JButton btn_undo;
    private javax.swing.JPopupMenu contexMenuOutput;
    private javax.swing.JPopupMenu contextMenuOptions;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.Box.Filler filler3;
    private javax.swing.Box.Filler filler4;
    private javax.swing.Box.Filler filler6;
    private javax.swing.JPanel jPanelOutput;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lbl_statusbarFilePath;
    private javax.swing.JLabel lbl_statusbarInformation;
    private javax.swing.JMenuItem mItemBigFont;
    private javax.swing.JMenuItem mItemCopyOutput;
    private javax.swing.JMenuItem mItemDeleteOutput;
    private javax.swing.JMenuItem mItemInterpret;
    private javax.swing.JMenuItem mItem_Compile;
    private javax.swing.JMenuItem mItem_CompileDll;
    private javax.swing.JMenuItem mItem_EditorOptions;
    private javax.swing.JMenuItem mItem_about;
    private javax.swing.JMenuItem mItem_exit;
    private javax.swing.JMenuItem mItem_help;
    private javax.swing.JMenuItem mItem_printFile;
    private javax.swing.JMenu menu_Boo;
    private javax.swing.JMenuItem menu_LejosOptions;
    private javax.swing.JPanel panelStatusBar;
    private javax.swing.JToolBar toolbar;
    private javax.swing.JTextArea txtArea_Output;
    // End of variables declaration//GEN-END:variables

    private void handleCloseTab() {
        if (this.jTabbedPane1.getSelectedComponent() != this.jPanelOutput) {
            this.jTabbedPane1.remove(this.jTabbedPane1.getSelectedIndex());
            if (!(this.jTabbedPane1.getSelectedIndex() == 0)) {
                this.activeTab = (CustomTabPanel) this.jTabbedPane1.getSelectedComponent();
            }
        }

    }

    private void handleSaveFileAsAction() {
        this.activeTab.handleSaveFileAs();
    }
    
    public void setStatusInfo(String pInfoText) {
        this.lbl_statusbarInformation.setText(pInfoText);
    }
//    private void addCloseButtonToTab(){
//        JPanel pnlTabHeader = new JPanel(new BorderLayout());
//        pnlTabHeader.setOpaque(false);
//        JLabel lblTitle = new JLabel("DasIstDieDatei.boo");
//        JLabel lblTabPicture = new JLabel(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png")));
//        JButton btnClose = new JButton();
//        //btnClose.setOpaque(false);
//        btnClose.setContentAreaFilled(false); 
//        //btnClose.setBorderPainted( false );
//        //btnClose.setBorder(new EmptyBorder(0,0,0,0));
//        btnClose.setSize(new Dimension(16,16));
//        btnClose.setPreferredSize(new Dimension(16,16));
//        btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
//        btnClose.setToolTipText("Close Action");
//        btnClose.setFocusable(false);
//        //btnClose.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
//        //btnClose.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
//        btnClose.addActionListener(new java.awt.event.ActionListener() {
//            @Override
//            public void actionPerformed(java.awt.event.ActionEvent evt) {
//                handleCloseFileAction();
//                System.out.println("Narf");
//            }
//        });
//        pnlTabHeader.add(lblTabPicture, BorderLayout.WEST);
//        pnlTabHeader.add(lblTitle,BorderLayout.CENTER);
//        pnlTabHeader.add(btnClose, BorderLayout.EAST);
//        this.jTabbedPane1.setTabComponentAt(1, pnlTabHeader);
//    }
}
